package com.xjrsoft.module.customerTwo.ycode.t_ycode_product.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.XjrUser;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.module.base.entity.XjrBaseUser;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.base.service.IXjrBaseUserService;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.dto.TYcodeProductDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.dto.SaveTYcodeProductFormDataDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.vo.TYcodeProductFormDataVo;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.vo.TYcodeProductListVo;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.dto.TYcodeProductListDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.vo.TYcodeProductDetailVo;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.entity.TYcodeProductDetail;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.dto.TYcodeProductDetailDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product_collect.entity.TYcodeProductCollect;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product_collect.service.ITYcodeProductCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.entity.TYcodeProduct;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.vo.TYcodeProductVo;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.service.ITYcodeProductService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2024-07-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("/t_ycode_product")
@Api(value = "产品管理", tags = "产品管理接口")
public class TYcodeProductController {


	private final ITYcodeProductService tYcodeProductService;

	private final IXjrBaseUserService iXjrBaseUserService;

	private final IXjrBaseCodeRuleService iXjrBaseCodeRuleService;

	private final String PRODUCTCODCONSTANT = "999";

	private final ITYcodeProductCollectService itYcodeProductCollectService;


	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tYcodeProduct")
	@SaCheckPermission(value = {"t_ycode_product:view", "t_ycode_product:modify"}, mode = SaMode.OR)
	public Response<TYcodeProductFormDataVo> getFormData(@PathVariable String id) {
		TYcodeProductFormDataVo formDataVo = new TYcodeProductFormDataVo();
		// 主表数据
		TYcodeProduct tYcodeProduct = tYcodeProductService.getById(id);
		TYcodeProductVo tYcodeProductVo = BeanUtil.copy(tYcodeProduct, TYcodeProductVo.class);
		formDataVo.setTYcodeProductVo(tYcodeProductVo);
		// 从表数据
		List<TYcodeProductDetailVo> tYcodeProductDetailVoList = BeanUtil.copyList(tYcodeProductService.getTYcodeProductDetailByParentId(id), TYcodeProductDetailVo.class);
		formDataVo.setTYcodeProductDetailVoList(tYcodeProductDetailVoList);
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tYcodeProduct")
		public Response<PageOutput<TYcodeProductListVo>> getPageList(TYcodeProductListDto listDto) {
		//如果不是超级管理员，默认只能查询自己商户的数据
		XjrUser currentUser = SecureUtil.getUser();
		if(!currentUser.isAdminUser()){
			listDto.setMerchant_id(currentUser.getMerchantId());
		}
		IPage<TYcodeProduct> page = tYcodeProductService.getPageList(listDto);
		List<TYcodeProductListVo> records = BeanUtil.copyList(page.getRecords(), TYcodeProductListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入tYcodeProduct")
	@SaCheckPermission("t_ycode_product:add")
	public Response save(@RequestBody SaveTYcodeProductFormDataDto formDto) {
		XjrUser currentUser = SecureUtil.getUser();
		TYcodeProductDto tYcodeProductDto = formDto.getTYcodeProductDto();
		TYcodeProduct tYcodeProduct = BeanUtil.copy(tYcodeProductDto, TYcodeProduct.class);
		List<TYcodeProductDetailDto> tYcodeProductDetailDtoList = formDto.getTYcodeProductDetailDto();
		List<TYcodeProductDetail> tYcodeProductDetailList = BeanUtil.copyList(tYcodeProductDetailDtoList, TYcodeProductDetail.class);
		//数据补充
		tYcodeProduct.setMerchantId(currentUser.getMerchantId());
		tYcodeProduct.setUserId(currentUser.getUserId());
		XjrBaseUser xjrBaseUser = iXjrBaseUserService.getById(currentUser.getMerchantId());
		if(null != xjrBaseUser){
			tYcodeProduct.setFirmName(xjrBaseUser.getFFirmname());
		}
		tYcodeProduct.setCode(this.getProductCod(tYcodeProduct.getClassCompleteCode(),currentUser.getMerchantId()));
		boolean isSuccess = tYcodeProductService.addTYcodeProduct(tYcodeProduct, tYcodeProductDetailList);
		return Response.status(isSuccess);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入tYcodeProduct")
	@SaCheckPermission("t_ycode_product:modify")
	public Response update(@PathVariable String id, @RequestBody SaveTYcodeProductFormDataDto formDto) {
		TYcodeProductDto tYcodeProductDto = formDto.getTYcodeProductDto();
		TYcodeProduct tYcodeProduct = BeanUtil.copy(tYcodeProductDto, TYcodeProduct.class);

		List<TYcodeProductDetailDto> tYcodeProductDetailDtoList = formDto.getTYcodeProductDetailDto();
		List<TYcodeProductDetail> tYcodeProductDetailList = BeanUtil.copyList(tYcodeProductDetailDtoList, TYcodeProductDetail.class);
		return Response.status(tYcodeProductService.updateTYcodeProduct(id, tYcodeProduct, tYcodeProductDetailList));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"t_ycode_product:batchDelete", "t_ycode_product:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(tYcodeProductService.delete(ids));
	}

	/**
	 * 修改状态
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "修改状态", notes = "传入tYcodeProduct")
	public Response submit(@RequestParam String id,@RequestParam String status) {
		TYcodeProduct entity = new TYcodeProduct();
		entity.setId(id);
		entity.setStatus(status);
		//如果是提交，修改提交人提交时间
		if("1".equals(status)){
			entity.setSubmitter(SecureUtil.getUser().getUserId());
			entity.setSubmitDate(LocalDateTime.now());
		}
		boolean isSuccess = tYcodeProductService.updateById(entity);
		return Response.status(isSuccess);
	}

	/**
	 * 产品编码规则
	 * @return
	 */
	private String getProductCod(String classCompleteCode,String merchantId){
		String code = PRODUCTCODCONSTANT+"."+classCompleteCode+"/";
		XjrBaseUser xjrBaseUser = iXjrBaseUserService.getById(merchantId);
		if(null != xjrBaseUser){
			code += xjrBaseUser.getFCreditcode();
		}
		code+="."+iXjrBaseCodeRuleService.genEncode("ProductSerialNumber");
		iXjrBaseCodeRuleService.useEncode("ProductSerialNumber");
		return code;
	}

	/**
	 * 新增产品收藏
	 */
	@PostMapping("/addCollect")
	@ApiOperation(value = "修改状态", notes = "传入tYcodeProduct")
	public Response addCollect(@RequestParam String product_id) {
		TYcodeProductCollect entity = new TYcodeProductCollect();
		entity.setProductId(product_id);
		entity.setUserId(SecureUtil.getUser().getUserId());
		entity.setCreateDate(LocalDateTime.now());
		entity.setId(IdWorker.get32UUID());
		boolean isSuccess = itYcodeProductCollectService.addTYcodeProductCollect(entity);
		return Response.status(isSuccess);
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/getCollectPage")
	@ApiOperation(value = "分页", notes = "传入tYcodeProduct")
	public Response<PageOutput<TYcodeProductListVo>> getCollectPage(TYcodeProductListDto listDto) {
		XjrUser currentUser = SecureUtil.getUser();
		List<TYcodeProductCollect> list = itYcodeProductCollectService.getUserCollect(currentUser.getUserId());
		if(CollectionUtil.isEmpty(list)){
			return Response.ok(null);
		}
		List<String> idList = new ArrayList<>();
		list.forEach(product -> idList.add(product.getId()));
		listDto.setProductIdList(idList);
		IPage<TYcodeProduct> page = tYcodeProductService.getCollectPageList(listDto);
		List<TYcodeProductListVo> records = BeanUtil.copyList(page.getRecords(), TYcodeProductListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

}
